﻿using DTOs;
using RBAC.Domain.Service.Interfae;
using RBAC.Repository.Interfae;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory;

namespace RBAC.Domain.Service.Impl
{
    public class MenuService : IMenuService
    {
        /// <summary>
        /// 菜单仓储
        /// </summary>
        private readonly IMenuRepository _menuRepository;
        /// <summary>
        /// 用户与角色仓储
        /// </summary>
        protected readonly IUserRoleRepository _UserRoleRepository;
        /// <summary>
        /// 角色与菜单仓储
        /// </summary>
        private readonly IRoleMenuRepository _roleMenuRepository;

        public MenuService(IMenuRepository menuRepository, IUserRoleRepository userRoleRepository, IRoleMenuRepository roleMenuRepository)
        {
            _menuRepository = menuRepository;
            _UserRoleRepository = userRoleRepository;
            _roleMenuRepository = roleMenuRepository;
        }
        /// <summary>
        /// 显示菜单（根据用户ID查菜单）
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        public async Task<PageDto<MenuDTOs>> GetMenusUserId(int uid, int pageSize, int pageIndex)
        {
            PageDto<MenuDTOs> pageDto = new PageDto<MenuDTOs>();
            var lists = await _menuRepository.GetAllAsync();

            var listobj = (from d in await _menuRepository.GetAllAsync()
                           join a in await _roleMenuRepository.GetAllAsync()
                           on d.MenuId equals a.MenuId
                           join b in await _UserRoleRepository.GetAllAsync()
                           on a.roleId equals b.roleId
                           where !d.MenuDelete && b.userInfoId == uid
                           select new MenuDTOs
                           {
                               MenuId = d.MenuId,
                               MenuName = d.MenuName,
                               MenuUrl = d.MenuUrl,
                               MenuPid = d.MenuPid,
                               MenuDelete = d.MenuDelete,
                               CreateBy = d.CreateBy,
                               CreateDate = d.CreateDate,
                               UpdateBy = d.UpdateBy,
                               UpdateDate = d.UpdateDate,
                               GetMenusdto = (from a in lists
                                              where a.MenuId == d.MenuPid
                                              select new MenuDto
                                              {
                                                  MenuName = a.MenuName,
                                              }).ToList()

                           }).Distinct();
            pageDto.ToalCount = listobj.Count();
            pageDto.PageCount = (int)Math.Ceiling(listobj.Count() * 1.0 / pageSize);
            pageDto.list = listobj.OrderByDescending(d => d.CreateDate).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            return pageDto;
           

          
        }
        /// <summary>
        /// 添加菜单
        /// </summary>
        /// <param name="menu"></param>
        /// <returns></returns>

        public async Task<int> AddMenu(Menu menu)
        {
            var menunames = await _menuRepository.GetAsync(d => d.MenuName == menu.MenuName);
            if (menunames.Count() > 0)
            {
                return -1;
            }
            return await _menuRepository.AddAsync(menu);
        }
        ///// <summary>
        ///// 删除
        ///// </summary>
        ///// <param name="Pid"></param>
        ///// <returns></returns>
        //public async Task<int> DelMenu(int ids)
        //{
        //    //确定ID
        //   var menlist=await _menuRepository.GetModelsAsync(ids);
        //    if (menlist == null)
        //    {
        //        return -1;
        //    }
        //    menlist.MenuDelete=true;
        //    //进行递归
        //    var child = await _menuRepository.GetAsync(d => d.MenuPid == menlist.MenuId);
        //    foreach (var item in child)
        //    {
        //        await DelMenu(item.MenuId);
        //    }
        //    return await _menuRepository.UpdateAsync(menlist);
        //}


        /// <summary>
        /// 删除前作判断是否有子集
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async Task<int> DelMenu13(int ids)
        {
            var menlist = await _menuRepository.GetAsync(d => d.MenuPid == ids);
            if(menlist.Count() > 0) 
            {
                return -1;
            }
            else
            {
 await DelMenu2(ids);
            return 0;
            }
           
        }
        /// <summary>
        /// 删除
        /// </summary>
        public async Task<int> DelMenu2(int ids)
        {
            //确定ID
            var menlist = await _menuRepository.GetModelsAsync(ids);
            menlist.MenuDelete = true;
            //进行递归
            var child = await _menuRepository.GetAsync(d => d.MenuPid == menlist.MenuId);
            foreach (var item in child)
            {
                await DelMenu2(item.MenuId);
            }
            return await _menuRepository.UpdateAsync(menlist);
        }




        /// <summary>
        /// 显示全部菜单
        /// </summary>
        /// <returns></returns>
        public async Task<List<Menu>> GetMenuList()
        {
            return await _menuRepository.GetAllAsync();
        }

        /// <summary>
        /// 显示
        /// </summary>
        /// <param name="pid"></param>
        /// <returns></returns>
        public async Task<List<MenuDTOs>> GetMenus()
        {
            var list = await _menuRepository.GetAllAsync();

            var listobj = (from d in await _menuRepository.GetAllAsync()
                       where !d.MenuDelete
                          select new MenuDTOs
                          {
                              MenuId = d.MenuId,
                              MenuName = d.MenuName,
                              MenuUrl = d.MenuUrl,
                              MenuPid = d.MenuPid,
                              MenuDelete = d.MenuDelete,
                              CreateBy = d.CreateBy,
                              CreateDate = d.CreateDate,
                              UpdateBy = d.UpdateBy,
                              UpdateDate = d.UpdateDate,
                              GetMenusdto = (from a in list where a.MenuId==d.MenuPid
                                            select new MenuDto
                                            {
                                                MenuName = a.MenuName,
                                            }).ToList()
                                           
                          }).ToList();


            return listobj;
        }

       

        /// <summary>
        /// 反填菜单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<Menu> GetMenuValue(int id)
        {
            return await _menuRepository.GetModelsAsync(id);
        }
        /// <summary>
        /// 修改菜单
        /// </summary>
        /// <param name="menu"></param>
        /// <returns></returns>
        public async Task<int> UpdateMenu(Menu menu)
        {
            
            var count = await _menuRepository.GetAsync(d => !d.MenuDelete && d.MenuPid == menu.MenuPid &&
            d.MenuName == menu.MenuName && d.MenuId != menu.MenuPid);
            if (count.Count()>0)
            {
                return -1;
            }
            else
            {
                return await _menuRepository.UpdateAsync(menu);
            }
          
        }
    }
}
